import re

ram_data = {}
with open("project.map", "r") as f:
    for line in f:
        if ".o" in line and "0x" in line and (".bss" in line or ".data" in line):
            parts = re.split(r"\s+", line.strip())
            file_name = parts[-1].split("/")[-1]  # 提取目标文件名
            print(parts)
            try:
                size = int(parts[3], 16)  # 十六进制转十进制
            except ValueError:  # 处理可能的异常
                print(f"Error: Could not convert size to integer: {parts[3]}")
                continue
            ram_data[file_name] = ram_data.get(file_name, 0) + size
# 按RAM占用排序
sorted_ram = sorted(ram_data.items(), key=lambda x: x[1], reverse=True)

print("按RAM占用排序:", sorted_ram)
